package com.kaili.db.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.pojo.dto.delivery.EwbsDeliveryAddDTO;
import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.pojo.dto.delivery.EwbsDeliveryEditDTO;
import com.kaili.pojo.entity.HsEwbsDelivery;
import com.kaili.pojo.entity.HsEwbsDeliveryDetail;
import com.kaili.pojo.entity.HsOptEwbAppoint;
import com.kaili.db.mapper.HsEwbsDeliveryMapper;
import com.kaili.db.mapper.HsOptEwbAppointMapper;
import com.kaili.db.service.IHsEwbsDeliveryDetailService;
import com.kaili.db.service.IHsEwbsDeliveryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.pojo.vo.AppointQueryVO;
import com.kaili.pojo.vo.DeliveryPointQueryVO;
import com.kaili.pojo.vo.DeliveryQueryVO;
import com.kaili.common.exception.BaseExcept;
import com.kaili.utils.DateUtils;
import com.kaili.common.util.SnowflakeIdFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cgm
 * @since 2018-05-08
 */
@Service("ewbsDeliveryService")
@Transactional
public class HsEwbsDeliveryServiceImpl extends ServiceImpl<HsEwbsDeliveryMapper, HsEwbsDelivery> implements IHsEwbsDeliveryService {

    @Autowired
    IHsEwbsDeliveryDetailService ewbsDeliveryDetailService;

    @Autowired
    HsOptEwbAppointMapper ewbAppointMapper;

    @Autowired
    ILoginUserInfoService loginUserInfoService;

    @Override
    public int addDelivery(EwbsDeliveryAddDTO addBean) throws BaseExcept {
        HsEwbsDelivery ewbsDelivery = new HsEwbsDelivery();
        BeanUtils.copyProperties(addBean,ewbsDelivery);

        String operationId = SnowflakeIdFactory.getNextId();
        ewbsDelivery.setOperationId(operationId);
        ewbsDelivery.setCreatedTime(DateUtils.getCurrentDate());
        ewbsDelivery.setCreatedBy(loginUserInfoService.getLoginUserId());
        ewbsDelivery.setSiteId(loginUserInfoService.getLoginSiteId());
        baseMapper.insert(ewbsDelivery);

        List<String> list =  addBean.getAppointNoList();
        List<HsEwbsDeliveryDetail> deliveryDetailList = new ArrayList<HsEwbsDeliveryDetail>();

        for (String appointNo:list) {
            HsEwbsDeliveryDetail ewbsDeliveryDetail = new HsEwbsDeliveryDetail();
            ewbsDeliveryDetail.setOperationId(operationId);
            ewbsDeliveryDetail.setAppointNo(appointNo);
            deliveryDetailList.add(ewbsDeliveryDetail);

            HsOptEwbAppoint ewbAppoint = new HsOptEwbAppoint();
            ewbAppoint.setAppointNo(appointNo);
            ewbAppoint.setIsHandle(1);
            ewbAppoint.setDeliveryTime(DateUtils.getCurrentDate());
            ewbAppointMapper.updateByAppointNo(ewbAppoint);

        }
        ewbsDeliveryDetailService.insertBatch(deliveryDetailList);
        return 0;
    }

    @Override
    public int updateDelivery(EwbsDeliveryEditDTO editDTO) throws BaseExcept {
        HsEwbsDelivery ewbsDelivery = new HsEwbsDelivery();
        BeanUtils.copyProperties(editDTO,ewbsDelivery);

        ewbsDelivery.setModifiedTime(DateUtils.getCurrentDate());
        ewbsDelivery.setModifiedBy(loginUserInfoService.getLoginUserId());
        baseMapper.updateById(ewbsDelivery);

        List<String> list =  editDTO.getAppointNoList();
        List<HsEwbsDeliveryDetail> deliveryDetailList = new ArrayList<HsEwbsDeliveryDetail>();

        if(CollectionUtils.isNotEmpty(list)){
            //查询原先关联的预约单号并修改这些预约单号的接货时间和处理状态
            List<HsEwbsDeliveryDetail>   oldList = ewbsDeliveryDetailService.selectByOperationId(editDTO.getOperationId());
            for (HsEwbsDeliveryDetail deliveryDetail:oldList) {
                HsOptEwbAppoint ewbAppoint = new HsOptEwbAppoint();
                ewbAppoint.setAppointNo(deliveryDetail.getAppointNo());
                ewbAppointMapper.updateDeliveryTimeByAppointNo(ewbAppoint);
            }

            //删除原先关联的预约单号
            ewbsDeliveryDetailService.deleteByOperationId(editDTO.getOperationId());
            for (String appointNo:list) {
                HsEwbsDeliveryDetail ewbsDeliveryDetail = new HsEwbsDeliveryDetail();
                ewbsDeliveryDetail.setOperationId(editDTO.getOperationId());
                ewbsDeliveryDetail.setAppointNo(appointNo);
                deliveryDetailList.add(ewbsDeliveryDetail);

                HsOptEwbAppoint ewbAppoint = new HsOptEwbAppoint();
                ewbAppoint.setAppointNo(appointNo);
                ewbAppoint.setIsHandle(1);
                ewbAppoint.setDeliveryTime(DateUtils.getCurrentDate());
                ewbAppointMapper.updateByAppointNo(ewbAppoint);
            }
            ewbsDeliveryDetailService.insertBatch(deliveryDetailList);
        }

        return 0;
    }

    @Override
    public Page<DeliveryQueryVO> queryDeliveryPageList(Page<DeliveryQueryVO> page, Map search) throws BaseExcept {
        return page.setRecords(baseMapper.queryDeliveryList(page, search));
    }

    @Override
    public List<AppointQueryVO> queryAppointListByDeliveryNo(Map search) throws BaseExcept {
        return baseMapper.queryAppointListByDeliveryNo(search);
    }

    @Override
    public List<DeliveryQueryVO> queryDeliveryList(Map<String,Object> searchMap) throws BaseExcept {
        return baseMapper.queryDeliveryList(searchMap);
    }

    @Override
    public DeliveryQueryVO queryDeliveryInfo(Map search) throws BaseExcept {
        return baseMapper.queryDeliveryInfo(search);
    }

    @Override
    public int doFinishDelivery(List<String> operationIdList) throws BaseExcept {
        for (String  operationId:operationIdList) {
            HsEwbsDelivery ewbsDelivery = new HsEwbsDelivery();
            ewbsDelivery.setOperationId(operationId);
            ewbsDelivery.setFinishTime(DateUtils.getCurrentDate());
            ewbsDelivery.setFinishBy(loginUserInfoService.getLoginUserId());
            ewbsDelivery.setIsFinish(1);
            baseMapper.updateById(ewbsDelivery);
        }

        return 0;
    }

    @Override
    public List<DeliveryPointQueryVO> getDeliveryPointList(Map<String, Object> searchMap) throws BaseExcept {
        return baseMapper.getDeliveryPointList(searchMap);
    }

    @Override
    public int deleteDelivery(String operationId) throws BaseExcept {
        List<String> no=baseMapper.appointNo(operationId);
        for(String appointNo:no){
            baseMapper.updateIsHandle(appointNo);
        }
        baseMapper.deleteDelivery(operationId);
        ewbsDeliveryDetailService.deleteByOperationId(operationId);
        return 0;
    }


}
